package com.xinou.daily_report.utils;

import com.xinou.common.util.SpringContextBeanUtil;
import com.xinou.daily_report.PO.ConfigChangePO;
import com.xinou.daily_report.enumerate.SysConfigEnum;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author lcc
 * @create 2021-06-24
 * @注意 本内容仅限于dev414内部传阅，禁止外泄以及用于其他的商业目的
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ConstUtil {

    private final StringRedisTemplate stringRedisTemplate;

    //不可修改值
    /**
     * 系统默认id
     */
    public static Long DEFAULT_SYSTEM_ID = 999999L;

    /**
     * 积分类型
     */
    public static final Integer INTEGRAL_TYPE_FLOWER_ADD_SCORE = 6;
    public static final Integer INTEGRAL_TYPE_DAILY_ADD_SCORE = 5;

    /**
     * //日报的状态
     */
    public static final Integer DAILY_NO_COMMENT = 0;
    public static final Integer DAILY_ALREADY_COMMENT = 1;
    public static final Integer DAILY_NO_REPLY = 1;
    public static final Integer DAILY_ALREADY_REPLY = 2;

    /**
     * ok单状态
     */
    // 正常
    public static final Integer OK_FORM_NORMAL = 0;
    // 完成
    public static final Integer OK_FORM_FINISHED = 1;
    // 超时
    public static final Integer OK_FORM_OVERTIME = 2;
    // 临期
    public static final Integer OK_FORM_APPROACH = 3;
    // 确认
    public static final Integer OK_FORM_CONFIRMED = 4;

    public static final Integer DAILY_PLAN_MAX_COUNT = 1;

    public static final String CONST_NAME = "config:constValues";

    @PostConstruct
    void init() {
        initialization(stringRedisTemplate);
    }

    public static List<SysConfigEnum> getMap() {
        return Arrays.asList(SysConfigEnum.values());
    }

    public static void set() {
        StringRedisTemplate redisTemplate = SpringContextBeanUtil.getBean(StringRedisTemplate.class);
        redisTemplate.opsForValue().set("config:assistant", "助理");
        redisTemplate.opsForValue().set("config:big-lead", "领导");
        redisTemplate.opsForValue().set("config:group-leader", "组长");
        redisTemplate.opsForValue().set("config:staff-member", "员工");
        redisTemplate.opsForSet().add("config:lead-role", "组长","助理", "领导");
        Map<Integer, Integer> constMap = new HashMap<>(16);
        constMap.put(SysConfigEnum.OK_FORM_APPROACH_DAY.ordinal(), 0);
        constMap.put(SysConfigEnum.DEFAULT_ADD_FRUIT.ordinal(), 10);
        constMap.put(SysConfigEnum.DEFAULT_MAX_SCORE_ADD_TIMES.ordinal(), 3);
        constMap.put(SysConfigEnum.DEFAULT_FLOWER_SCORE.ordinal(), 3);
        constMap.put(SysConfigEnum.LEAST_COMMENT_TIMES.ordinal(), 3);
        constMap.put(SysConfigEnum.GIVE_FLOWER_MAX_TIMES.ordinal(), 3);
        constMap.put(SysConfigEnum.DAILY_DEFAULT_ADD_FRUIT.ordinal(), 10);
        constMap.put(SysConfigEnum.WEEK_PLAN_DEFAULT_ADD_FRUIT.ordinal(), 10);
        constMap.put(SysConfigEnum.WEEK_RESULT_DEFAULT_ADD_FRUIT.ordinal(), 10);
        redisTemplate.opsForHash().putAll(CONST_NAME, constMap);
    }

    public static void restLeadRole(ConfigChangePO po) {
        StringRedisTemplate redisTemplate = SpringContextBeanUtil.getBean(StringRedisTemplate.class);
        redisTemplate.opsForHash().put(CONST_NAME, po.getKey().toString(), po.getValue().toString());
        initialization(redisTemplate);
    }

    static void initialization(StringRedisTemplate stringRedisTemplate) {
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(CONST_NAME);
        Map<Integer, Integer> constMap = new HashMap<>(entries.size());
        entries.forEach((key, value) -> constMap.put(Integer.valueOf(key.toString()), Integer.valueOf(value.toString())));
        SysConfigEnum.OK_FORM_APPROACH_DAY.setValue(constMap.getOrDefault(SysConfigEnum.OK_FORM_APPROACH_DAY.ordinal(), 0));
        SysConfigEnum.DEFAULT_ADD_FRUIT.setValue(constMap.getOrDefault(SysConfigEnum.DEFAULT_ADD_FRUIT.ordinal(), 10));
        SysConfigEnum.DEFAULT_MAX_SCORE_ADD_TIMES.setValue(constMap.getOrDefault(SysConfigEnum.DEFAULT_MAX_SCORE_ADD_TIMES.ordinal(), 3));
        SysConfigEnum.DEFAULT_FLOWER_SCORE.setValue(constMap.getOrDefault(SysConfigEnum.DEFAULT_FLOWER_SCORE.ordinal(), 3));
        SysConfigEnum.LEAST_COMMENT_TIMES.setValue(constMap.getOrDefault(SysConfigEnum.LEAST_COMMENT_TIMES.ordinal(), 3));
        SysConfigEnum.GIVE_FLOWER_MAX_TIMES.setValue(constMap.getOrDefault(SysConfigEnum.GIVE_FLOWER_MAX_TIMES.ordinal(), 3));
        SysConfigEnum.DAILY_DEFAULT_ADD_FRUIT.setValue(constMap.getOrDefault(SysConfigEnum.DAILY_DEFAULT_ADD_FRUIT.ordinal(), 10));
        SysConfigEnum.WEEK_PLAN_DEFAULT_ADD_FRUIT.setValue(constMap.getOrDefault(SysConfigEnum.WEEK_PLAN_DEFAULT_ADD_FRUIT.ordinal(), 10));
        SysConfigEnum.WEEK_RESULT_DEFAULT_ADD_FRUIT.setValue(constMap.getOrDefault(SysConfigEnum.WEEK_RESULT_DEFAULT_ADD_FRUIT.ordinal(), 10));
    }
}
